package com.vito.framework.system.feature.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.vito.framework.base.controller.BaseController;
import com.vito.framework.base.exception.CommonException;
import com.vito.framework.system.feature.model.Feature;
import com.vito.framework.system.feature.service.FeatureService;
import com.vito.support.ApiResponse;
import com.vito.support.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 作者：包子
 * 时间：2020/10/9 14:56
 * 描述：
 */
@RestController
@RequestMapping("${adminPath}/feature")
public class FeatureController extends BaseController {
    @Autowired
    private FeatureService featureService;

    @PostMapping(value = "list", consumes = "application/json;charset=UTF-8")
    public ApiResponse list(HttpServletRequest request, @RequestBody JSONObject params) {
        Map<String, Object> paramMap = super.getParamMap(request, params);
        if (null == paramMap) {
            return ApiResponse.of(ResultEnum.PARAMS_INVALID);
        }
        String param_keyword = params.getString("param_keyword");
        String param_type = params.getString("param_type");
        String sence = params.getString("sence");
        int page = params.getIntValue("page");
        int limit = params.getIntValue("limit");
        paramMap.put("param_curpage", page);
        paramMap.put("param_ipagecount", limit);
        if (!StrUtil.isEmpty(param_type)) {
            paramMap.put("param_type", param_type);
        }
        if (!StrUtil.isEmpty(param_keyword)) {
            paramMap.put("param_keyword", param_keyword);
        }
        if (!StrUtil.isEmpty(sence)) {
            paramMap.put("scene", sence);
        }
        return ApiResponse.ofPage(featureService.list(paramMap));
    }

    @PostMapping(value = "addFeature", consumes = "application/json;charset=UTF-8")
    public ApiResponse addFeature(HttpServletRequest request, @RequestBody Feature feature) {
        if (null == feature) {
            return ApiResponse.of(ResultEnum.PARAMS_INVALID);
        }
        try {
            featureService.save(feature);
            return ApiResponse.ok(ResultEnum.OK);
        } catch (CommonException e) {
            return ApiResponse.of(e);
        }
    }

    @PostMapping(value = "updateFeature", consumes = "application/json;charset=UTF-8")
    public ApiResponse updateFeature(HttpServletRequest request, @RequestBody Feature feature) {
        if (null == feature) {
            return ApiResponse.of(ResultEnum.PARAMS_INVALID);
        }
        try {
            featureService.update(feature, new LambdaUpdateWrapper<Feature>().eq(Feature::getId, feature.getId()));
            return ApiResponse.ok(ResultEnum.OK);
        } catch (CommonException e) {
            return ApiResponse.of(e);
        }
    }

    @PostMapping(value = "getById", consumes = "application/json;charset=UTF-8")
    public ApiResponse getById(HttpServletRequest request, @RequestBody JSONObject params) {
        Feature feature = null;
        try {
            Integer id = params.getInteger("id");
            feature = featureService.get(id);
        } catch (CommonException e) {
            e.printStackTrace();
        }
        return ApiResponse.ok(feature);
    }

    @PostMapping(value = "deleteById", consumes = "application/json;charset=UTF-8")
    public ApiResponse deleteById(HttpServletRequest request, @RequestBody JSONObject params) {
        Integer id = params.getInteger("id");
        try {
            featureService.removeById(id);
            return ApiResponse.ok(ResultEnum.OK);
        } catch (CommonException e) {
            return ApiResponse.of(e);
        }
    }

}
